--[[
FilePath: \Lua\View\UIHeroManor\UIHeroManorMoveInSuccess.lua
城市界面入驻成功
--]]

require 'View/UIBase/UIBase'

UIHeroManorMoveInSuccess = UIBase.New(PanelNames.UIHeroManorMoveInSuccess)
local _M = UIHeroManorMoveInSuccess

_M.hasInit = false
_M.RoleLihui = nil
_M.drawWrapper = nil

function _M.Init(cityId, index)
    _M.cityId = cityId or 1;
    _M.selectIndex = index or 1;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgclose"), self.OnBack);
        self.heroPos = self:GetChild("HeroPos");
        self.tips = self:GetChild("Tips");
        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()

end

function _M:removeEvent()

end

-- 显示
function _M:OnEnable()
    self:RefreshView();
end

function _M:RefreshView()
    local manorInfo = logicMgr.HeroManorManager.getManorInfo(self.cityId, self.selectIndex);
    if manorInfo.masterId == nil or tonumber(manorInfo.masterId) == 0 then
        self:OnBack();
    else
        local heroInfo = logicMgr.HeroManager.getHeroDatabyPartnerId(manorInfo.masterId, true);
        self.tips.text = string.format(getLanguage("HeroManorText08"), heroInfo.name);
        local useSkinId = logicMgr.SkinManager.getHeroSkinId(heroInfo.partnerModelId);
        local path = logicMgr.SkinManager.GetHeroDrawPathBySkinId(useSkinId);
        local heroCfg = nil;
        if useSkinId and tonumber(useSkinId) > 0 then
            heroCfg = dataTable.getDataTableByStringId("t_skin", useSkinId);
        end
        if heroCfg == nil then
            heroCfg = dataTable.getDataTableByStringId("t_hero", heroInfo.partnerModelId);
        end
        local sc = 1;
        if heroCfg == nil then
            sc = decodeJsonStrToTabel(heroCfg.f_Resize)[1];
        end

        self.drawWrapper = GoWrapper.New();
        resMgr:LoadModelPrefab(path, function(go)
            if(self.RoleLihui~=nil)then
                destroyImmediate(self.RoleLihui);
                self.RoleLihui = nil
            end
            self.RoleLihui = newObject(go);
            self.RoleLihui.name = path;
            Util.AddAction(self.RoleLihui,"idle1",true,0.5);
            self.RoleLihui:AddComponent(TCanvas);
            self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
            Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,0,0,0)
            Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,sc,sc,sc)
            self.heroPos:SetNativeObject(self.drawWrapper);
        end)
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self.drawWrapper then
        self.drawWrapper:Dispose();
        self.drawWrapper = nil;
    end
    self.hasInit = false
end

return _M;